---
title: 'clearLocalStorage | Cypress Documentation'
description: Clear data in localStorage for current domain and subdomain in Cypress.
sidebar_label: clearLocalStorage
---

<ProductHeading product="app" />

# clearLocalStorage

Clear data in localStorage for current domain and subdomain.

:::caution

Cypress automatically clears all local storage _before_ each test to prevent
state from being shared across tests when
[test isolation](/app/core-concepts/writing-and-organizing-tests#Test-Isolation)
is enabled. You shouldn't need to use this command unless you're using it to
clear localStorage inside a single test or test isolation is disabled.

:::

## Syntax

```javascript
cy.clearLocalStorage()
cy.clearLocalStorage(key)
cy.clearLocalStorage(options)
cy.clearLocalStorage(keys, options)
```

### Usage

<Icon name="check-circle" color="green" /> **Correct Usage**

```javascript
cy.clearLocalStorage() // clear all local storage
```

### Arguments

<Icon name="angle-right" /> **keys _(String, RegExp)_**

Specify key to be cleared in localStorage.

<Icon name="angle-right" /> **options _(Object)_**

Pass in an options object to change the default behavior of
`cy.clearLocalStorage()`.

| Option | Default | Description                                                                         |
| ------ | ------- | ----------------------------------------------------------------------------------- |
| `log`  | `true`  | Displays the command in the [Command log](/app/core-concepts/open-mode#Command-Log) |

<HeaderYields />

- `cy.clearLocalStorage()` yields the localStorage for the current domain.

## Examples

### No Args

#### Clear all localStorage

```javascript
cy.clearLocalStorage()
```

### Specific Key

#### Clear localStorage with the key 'appName'

```javascript
cy.clearLocalStorage('appName')
```

#### Clear all localStorage matching `/app-/` RegExp

```javascript
cy.clearLocalStorage(/app-/)
```

## Rules

<HeaderRequirements />

- `cy.clearLocalStorage()` requires being chained off of `cy`.

<HeaderAssertions />

- `cy.clearLocalStorage()` will only run assertions you have chained once, and
  will not [retry](/app/core-concepts/retry-ability).

<HeaderTimeouts />

- `cy.clearLocalStorage()` cannot time out.

## Command Log

**_Clear all localStorage_**

```javascript
cy.clearLocalStorage(/prop1|2/).then((ls) => {
  expect(ls.getItem('prop1')).to.be.null
  expect(ls.getItem('prop2')).to.be.null
  expect(ls.getItem('prop3')).to.eq('magenta')
})
```

The commands above will display in the Command Log as:

<DocsImage
  src="/img/api/clearlocalstorage/clear-ls-localstorage-in-command-log.png"
  alt="Command log for clearLocalStorage"
/>

When clicking on `clearLocalStorage` within the command log, the console outputs
the following:

<DocsImage
  src="/img/api/clearlocalstorage/local-storage-object-shown-in-console.png"
  alt="console.log for clearLocalStorage"
/>

## See also

- [`cy.clearCookies()`](/api/commands/clearcookies)
